import { defineStore } from 'pinia'

interface StateType {
  testData: string,
  socket: WebSocket | null, // 添加 WebSocket 实例
}


export const useStoreTemplate = defineStore('template', {
  state: (): StateType => ({
    testData: localStorage.getItem('testData') || '',
    socket: null, // 初始化 WebSocket 实例为 null
  }),

  getters: {
    getTest: (state): string => state.testData,
    getTest1(): string {
      return this.testData
    }
  },
  actions: {
    TEST_ACTION(data: string) {
      this.testData = data
      localStorage.setItem('testData', data)
    },
    // 新增接收消息的动作
    handleSocketMessage(event: MessageEvent) {
      const message = event.data;
      console.log("接收到的消息:", message);
      // 这里可以根据需要处理接收到的消息
      // 例如，更新状态或触发其他动作
    },
  }
})
